# language: zh-CN
# 需求ID: REQ-009
# SRS需求: 3.4 校验规则映射（Must）
# Feature ID: Epic 9.1
# 用户故事: US-09-0001, US-09-0002, US-09-0003, US-09-0004
功能: 验证规则映射器
  作为开发者
  我希望ValidationMapper能将DSL验证规则正确映射为可执行的验证器
  以便确保数据质量和业务规则的自动执行

  背景:
    假设我是一个开发者
    并且我需要将DSL中的验证规则转换为运行时验证器
    并且我希望支持多种验证规则类型

  # US-09-0001: 基础字段验证规则映射
  场景: 映射基础验证规则为TypeORM装饰器
    假设我有一个包含基础验证规则的领域模型:
      """
      {
        "entities": {
          "User": {
            "attributes": {
              "name": {
                "type": "string",
                "rules": ["required", {"minLength": 2}, {"maxLength": 50}]
              },
              "email": {
                "type": "string", 
                "rules": ["required", {"format": "email"}, "unique"]
              },
              "age": {
                "type": "number",
                "rules": [{"min": 0}, {"max": 150}]
              },
              "phone": {
                "type": "string",
                "rules": [{"regex": "^\\+?[1-9]\\d{1,14}$"}]
              }
            }
          }
        }
      }
      """
    当我调用ValidationMapper.mapToTypeORMDecorators()时
    那么应该返回正确的基础验证装饰器:
      """
      {
        "User": {
          "name": [
            {"name": "IsNotEmpty"},
            {"name": "MinLength", "args": [2]},
            {"name": "MaxLength", "args": [50]}
          ],
          "email": [
            {"name": "IsNotEmpty"},
            {"name": "IsEmail"},
            {"name": "IsUnique"}
          ],
          "age": [
            {"name": "Min", "args": [0]},
            {"name": "Max", "args": [150]}
          ],
          "phone": [
            {"name": "Matches", "args": ["^\\+?[1-9]\\d{1,14}$"]}
          ]
        }
      }
      """

  # US-09-0002: 枚举值验证规则映射
  场景: 映射枚举验证规则
    假设我有一个包含枚举验证的领域模型:
      """
      {
        "entities": {
          "User": {
            "attributes": {
              "status": {
                "type": "string",
                "rules": [{"enum": "active,inactive,pending"}]
              },
              "role": {
                "type": "string", 
                "rules": [{"enum": "admin,user,guest"}]
              }
            }
          }
        }
      }
      """
    当我调用ValidationMapper.mapToTypeORMDecorators()时
    那么应该返回正确的枚举验证装饰器:
      """
      {
        "User": {
          "status": [
            {"name": "IsIn", "args": [["active", "inactive", "pending"]]}
          ],
          "role": [
            {"name": "IsIn", "args": [["admin", "user", "guest"]]}
          ]
        }
      }
      """

  # US-09-0003: 自定义验证器生成
  场景: 生成自定义验证器代码
    假设我有一个包含自定义验证规则的领域模型:
      """
      {
        "entities": {
          "User": {
            "attributes": {
              "username": {
                "type": "string",
                "rules": [{"custom": "uniqueUsername"}]
              },
              "password": {
                "type": "string",
                "rules": [{"custom": "strongPassword"}]
              }
            }
          }
        }
      }
      """
    当我调用ValidationMapper.generateCustomValidators()时
    那么应该生成自定义验证器代码:
      """
      [
        {
          "name": "UniqueUsernameValidator",
          "code": "import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';\n\n@ValidatorConstraint({ name: 'uniqueUsername', async: false })\nexport class UniqueUsernameValidator implements ValidatorConstraintInterface {\n  validate(value: any, args: ValidationArguments) {\n    // TODO: 实现 uniqueUsername 验证逻辑\n    return true;\n  }\n\n  defaultMessage(args: ValidationArguments) {\n    return 'uniqueUsername validation failed';\n  }\n}",
          "dependencies": ["class-validator"]
        },
        {
          "name": "StrongPasswordValidator", 
          "code": "import { ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments } from 'class-validator';\n\n@ValidatorConstraint({ name: 'strongPassword', async: false })\nexport class StrongPasswordValidator implements ValidatorConstraintInterface {\n  validate(value: any, args: ValidationArguments) {\n    // TODO: 实现 strongPassword 验证逻辑\n    return true;\n  }\n\n  defaultMessage(args: ValidationArguments) {\n    return 'strongPassword validation failed';\n  }\n}",
          "dependencies": ["class-validator"]
        }
      ]
      """

  # US-09-0004: 客户端验证规则导出
  场景: 导出客户端验证规则
    假设我有一个完整的领域模型
    当我调用ValidationMapper.exportClientValidationRules()时
    那么应该返回客户端可用的验证规则:
      """
      [
        {
          "field": "User.name",
          "rules": [
            {"type": "required", "message": "姓名不能为空"},
            {"type": "minLength", "value": 2, "message": "姓名长度不能少于2个字符"},
            {"type": "maxLength", "value": 50, "message": "姓名长度不能超过50个字符"}
          ]
        },
        {
          "field": "User.email",
          "rules": [
            {"type": "required", "message": "邮箱不能为空"},
            {"type": "email", "message": "邮箱格式不正确"}
          ]
        }
      ]
      """

  场景: 生成多语言验证消息
    假设我需要支持中英文验证消息
    当我调用ValidationMapper.generateMultiLanguageMessages()时
    那么应该返回多语言消息配置:
      """
      [
        {
          "key": "User.name.required",
          "messages": {
            "en": "name is required",
            "zh": "name是必填项"
          }
        },
        {
          "key": "User.email.required",
          "messages": {
            "en": "email is required",
            "zh": "email是必填项"
          }
        },
        {
          "key": "User.email.format",
          "messages": {
            "en": "email must be a valid email address",
            "zh": "email必须是有效的邮箱地址"
          }
        }
      ]
      """

  场景: 处理复杂验证规则组合
    假设我有一个包含复杂验证规则的字段:
      """
      {
        "entities": {
          "User": {
            "attributes": {
              "password": {
                "type": "string",
                "rules": [
                  "required",
                  {"minLength": 8},
                  {"maxLength": 128},
                  {"regex": "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)"},
                  {"custom": "noCommonPasswords"}
                ]
              }
            }
          }
        }
      }
      """
    当我调用ValidationMapper.mapToTypeORMDecorators()时
    那么应该正确处理所有验证规则:
      """
      {
        "User": {
          "password": [
            {"name": "IsNotEmpty"},
            {"name": "MinLength", "args": [8]},
            {"name": "MaxLength", "args": [128]},
            {"name": "Matches", "args": ["(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)"]}
          ]
        }
      }
      """
    并且应该生成相应的自定义验证器

  场景: 验证规则映射错误处理
    假设我有一个包含无效验证规则的领域模型:
      """
      {
        "entities": {
          "User": {
            "attributes": {
              "field1": {
                "type": "string",
                "rules": [{"invalidRule": "value"}]
              }
            }
          }
        }
      }
      """
    当我调用ValidationMapper.mapToTypeORMDecorators()时
    那么应该忽略无效的验证规则
    并且应该记录警告信息
    并且应该继续处理其他有效规则

  场景: 验证器缓存和重用
    假设我多次调用ValidationMapper的方法
    当处理相同的自定义验证规则时
    那么应该重用已生成的验证器
    并且应该避免重复生成相同的代码
    并且应该提供获取已缓存验证器的方法